package com.caifu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.caifu.pojo.SysMenu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import sun.security.util.Password;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 Mapper 接口
 * </p>
 *
 * @author Lyf
 * @since 2020-07-07
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    @Select("select a.* from sys_menu a left join sys_menu b on a.parent_no = b.menu_no where b.parent_no = 1 and a.menu_no != 55 order by a.sort")
    List<Map<String,Object>> getMenuAll();

    @Select("select a.* from sys_menu a left join sys_menu b on a.parent_no = b.menu_no where b.parent_no = 1 and a.menu_no != 2 and a.menu_no != 3 and a.menu_no != 4 and a.menu_no != 55 and a.menu_no != 99 order by a.sort")
    List<Map<String,Object>> getPasswordMenu();

    @Select("select a.* from sys_menu a left join sys_menu b on a.parent_no = b.menu_no where b.parent_no = 1 and a.menu_no != 2 and a.menu_no != 3 and a.menu_no != 4 and a.menu_no != 99 order by a.sort")
    List<Map<String,Object>> getAuditMenu();

    //获取用户赋权角色的二级菜单列表
    @Select("select m.MENU_NO,m.NAME,m.URL,m.PERMISSION_CODE,m.PARENT_NO,m.ICON from sys_menu m where m.menu_no in( select m.PARENT_NO from sys_role_menu rm left join sys_user_role ur on rm.role_id=ur.role_id left join sys_role r on rm.role_id=r.id left join sys_menu m on rm.menu_id=m.MENU_NO where ur.user_id is not null and ur.user_id=#{userNo}) and m.PARENT_NO=#{parentNo}")
    List<Map<String,Object>> getUserMenu(@Param("parentNo")int parentNo,@Param("userNo")int userNo);

    @Select("select a.PERMISSION_CODE,c.role_id from sys_menu a left join sys_role_menu b on a.menu_no = b.menu_id left join sys_user_role c on b.role_id = c.role_id where c.user_id = #{userNo}")
    List<Map<String,Object>> getUserPermissionMenu(@Param("userNo")int userNo);

    //获取用户赋权过的平台模块集合
    @Select("select GROUP_CONCAT(DISTINCT m.PARENT_NO) from sys_menu m where m.MENU_NO in ( select m.PARENT_NO from sys_menu m where m.MENU_NO in ( select m.PARENT_NO from sys_role_menu rm left join sys_user_role ur on rm.role_id=ur.role_id left join sys_menu m on m.MENU_NO=rm.menu_id where ur.user_id is not null and ur.user_id=#{userNo}))")
    String getUserPlatSet(@Param("userNo")int userNo);


    /**
     * 获取登录用户 菜单 按钮 权限
     * @param userNo  userNo
     * @return SysMenu
     */
    List<SysMenu> getMenuPermissionCodeByUserNo(String userNo);




}
